<?php
include("Libraries/Dal/DB_Queries.php");  

function login_user($username,$password)
{
	if($username!="" && $password!="")
	{
		$len_user=strlen($username);
		$len_pass=strlen($password);
		if($len_user<30 && $len_pass<30)
			{
				if(login($username,$password)==true)//klisi function apo tier1
				{   
					$pin=array("status"=>statheres::OLA_OK,"value"=>null);
					return $pin;
				}
				else
				{
					$pin=array("status"=>statheres::WRONG_PASS_OR_USER,"value"=>null);
					return $pin;
				}
			}	
		else
		{
			$pin=array("status"=>statheres::LARGE_FIELD,"value"=>null);
			return $pin;
		}
	}
	else
		{
			$pin=array("status"=>statheres::EMPTY_FIELD,"value"=>null);
			return $pin;;
		}
}	
	
//#############################################################################	

function logout()
{  
	unset($_SESSION['user']);
}	
	
//#############################################################################	
	
	
function register_member($username,$password,$email)
{
		if($username!="" && $password!="" && $email!="")
		{
			$len_user=strlen($username);
			$len_pass=strlen($password);
			$len_email=strlen($email);
			
			if($len_user<30 && $len_pass<30 && $len_email<50)
			{
				if (check_user_exists($username)==false)
				{
					 if (check_email_exists($email)==false)
					{			
					 	insert_into_member($username,$password,$email,"member");//klisi function apo tier1
						$pin=array("status"=>statheres::OLA_OK,"value"=>null);
						return $pin;
					}
					else
					{
						$pin=array("status"=>statheres::EMAIL_EXISTS,"value"=>null);
						return $pin;
					}
				}
				else
				{
					$pin=array("status"=>statheres::USER_EXISTS,"value"=>null);
					return $pin;
				}
			}
			else
			{
				$pin=array("status"=>statheres::LARGE_FIELD,"value"=>null);
				return $pin;
			}
		}
		else
		{
			$pin=array("status"=>statheres::EMPTY_FIELD,"value"=>null);
			return $pin;
		}		
}


//#############################################################################
//den einai xwrismeni se tiers
function update_member($user,$pass,$email,$member_type)
{
		if($user!="" && $pass!="" && $email!="")
		{
	  		$query="update member set username='$user',password='$pass',email='$email',type_id='$member_type' where username='$user'";
	  		$result=exec_sql_query($query);
	  		echo "<script>alert('Changes saved!')</script>";
			echo "<noscript><center><h4><font color='red'>Changes Saved</p></h4></font></noscript>";
		}
		else
		{
			echo "<script>alert('Fill all fields!')</script>";
			echo "<noscript><center><h4><font color='red'>Fill all fields!</p></h4></font></noscript>";
		}
}

//#############################################################################

function fill_dropdown()
{
	$result=get_username();
	$options="";
			
	while ($row=mysql_fetch_array($result))
	 {
		$user=$row["username"];
		$options.="<OPTION VALUE=\"$user\">".$user;
	 }
	echo "<SELECT NAME=\"user\">";
	echo "<OPTION VALUE=0>Choose".$options;
	echo "</SELECT>";
}

//#############################################################################

function delete_user($user)
{
	$userid=get_member_id($user);
	delete_comment_member($userid);
	delete_rate_member($userid); 
	delete_member($userid);
}

//#############################################################################

function check_user_admin($username)
{
	if(check_admin($username)=="1")
	{
		return true;
	}
	else
		return false;
	
}	
?>
